﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using GOAP.Entities;

namespace GOAP.Actions
{
    using GoapDataType = Dictionary<string, string>;
    using ConnectionType = List<string>;

    class MoveTo : Action
    {
        protected string target = "";

        public MoveTo()
        {
            name = "MoveTo";
        }

        override public List<Action> getActions(World world, Entity ent)
        {
            GoapDataType data = ent.getGoapData();
            //check for positional data
            if (data.ContainsKey("node"))
            {
                Node n = world.getEntity<Node>(data["node"]);
                if (n == null) return null; //Early out if node is faulty

                List<Action> actionList = new List<Action>();
                ConnectionType connections = n.getConnections();

                foreach (string node in connections)
                {
                    MoveTo move = new MoveTo();
                    move.target = node;

                    actionList.Add(move);
                }

                return actionList;
            }
            
            //No movement possible on agent. Action assigned to faulty agent?
            return null;
        }

        override public void goapAct(World world, Entity ent)
        {
            Entity targetEnt = world.getEntity<Entity>(ent.name); //Get entity of world (this is done so an action can be performed on the clone world for planning)
            Act(targetEnt);
        }

        override public void Act(Entity ent)
        {
            GoapDataType data = ent.getGoapData();
            data["node"] = target;
        }

        override public void print()
        {
            System.Console.WriteLine("  " + name + " -> " + target);
        }

        override public void copyMembers(Action copy)
        {
            base.copyMembers(copy);
            MoveTo move = copy as MoveTo;
            move.target = target;
        }

        override public Action clone()
        {
            MoveTo copy = new MoveTo();
            copyMembers(copy);
            return copy;
        } 
    }
}
